<?php
	/**
	* Real Estate Request Module
	*/
	class requestmodule {
		function name() { return "Request Module"; }
		function description() { return "Support request register on website"; }
		function author() { return "ABSoft.vn"; }

		function hasSources() { return true; }
		function hasContent() { return true; }
		function hasViews() { return true; }

		function supportsWorkflow() { return false; }

		function retPermissions()
		{
			return array('administrate','configure','approve','edit','delete', "manage");  
		}

		function permissions($internal = "") {
			return array(
			'administrate'=>'Administrate',
			'configure'=>'Configure',
			'edit'=>'Edit Request',
			'delete'=>'Delete Request',
			'approve'=>'Approve',
			'add_calendar'=>'Add to calendar',
			'manage'=>'Manage Request',
			);
		}

		function show($view,$loc = null, $title = "") {
			global $db;

			$config = $db->selectObject('requestmodule_config',"location_data='".serialize($loc)."'");
			if ($config == null) {
				echo "You should configure this module first";
			}
			else
			{
				// get default value (for logged in and not logged in)
				$user_info=null;
				if (exponent_users_isLoggedIn())
				{
					global $user;
					$user_info->default_name = "{$user->firstname} {$user->lastname}";
					$user_info->phone = $user->user_address->denomination;
					$user_info->email=$user->email;
				}

				$calendar_loc=null;
				$calendar_loc->mod="calendarmodule";
				$calendar_loc->src=$config->calendar_src;
				$calendar_loc->int="";

				$cats=$db->selectObjects("category", "location_data = '".serialize($calendar_loc)."' ORDER BY rank");
				// for category price
				foreach ($cats as $k=>$v)
					$v->cat_value_text=number_format(floatval($v->cat_value),0,",",".");

				// feb 11, 2011 - trungdt - end
				$template = new template('requestmodule',$view,$loc);
				$template->assign('config',$config);
				$template->assign('user_info',$user_info);
				$template->assign('moduletitle',$title);
				// return from_date and to_date
				$temp=aboid_getCurrentTime()+3600;
				$template->assign('from_date',date("n/j/Y", $temp));
				$template->assign('cats',$cats);
				$template->assign('to_date',date("n/j/Y G:i:s", $temp+3600));
				$template->assign('is_logged',exponent_users_isLoggedIn()?1:0);
				$template->register_permissions($this->retPermissions(),$loc);
				$template->output();
			}		

		}

		function deleteIn($loc) {
			global $db;
			$db->delete('requestmodule_request',"location_data='".serialize($loc)."'");
			$db->delete('requestmodule_config',"location_data='".serialize($loc)."'");
		}

		function copyContent($oloc,$nloc) {
			foreach ($db->selectObjects('requestmodule_request',"location_data='".serialize($oloc)."'") as $l) {
				$l->location_data = serialize($nloc);
				$db->insertObject($l,'requestmodule_request');
			}
		}

		function searchName() {
			return false; // this module does not support search
		}

		// This function return date format for module rsrequest
		// request = 1: Yêu cầu cách đây
		// request = 0: respone , trả lời cách đây
		function _dateFormat($date_time, $request=1, $short_mode=false)
		{
			$now=time();  
			$date_translate = exponent_lang_loadFile('subsystems/forms/controls/yuicalendarcontrol.php');
			if ($now - $date_time < 86400 )  // 24 hours
			{

				$un_equal=$now-$date_time;
				$uh=floor($un_equal / 3600);
				$un_equal=$un_equal-3600*$uh;

				$um=floor($un_equal / 60);
				$un_equal=$un_equal-60*$um; 

				$date_time="";
				if ($request == 1)
					$date_time = $date_translate['request'];
				else
					$date_time = $date_translate['answer'];
				if ($uh > 0)
				{
					$date_time.="{$uh} " . $date_translate['hour'];
				}

				if ($um > 0)
				{
					$date_time.="{$um} " . $date_translate['minute'];
				}

				if ($un_equal > 0)
				{
					$date_time.="{$un_equal} " . $date_translate['sec'];
				}
			}
			else
			{   
				if ($short_mode == true)
				{
					$date_time = date('d/m/Y H:i', $date_time);
				}
				else
				{
					$datetime = date('l, F d, o g:i a', $date_time);
					// If the language is english, use the default language of yui calendar
					if (USE_LANG != 'eng_US') {
						// Translate days of the week
						// thứ
						$datetime=$date_translate[strtolower(substr(date('l',$date_time),0,2))].", ";
						// ngày
						$datetime.=date('d',$date_time)." ";
						// tháng
						$datetime.=$date_translate[strtolower(date('F',$date_time))]." ";
						// năm
						$datetime.=date('o',$date_time)."; ";
						$datetime.=date('g:i',$date_time)." ";
						// am - pm
						$datetime.=$date_translate[strtolower(date('a',$date_time))];
						$date_time = $datetime;
					}
				}
			}
			return $date_time;
		}

		static function _BuildBody_for_calendar($rs_request)
		{
			$body="
			<p><b>Tên người đặt thuê</b>: %name%</p>
			<p><b>Số điện thoại liên hệ</b>: %phone%</p>
			<p><b>Email liên hệ</b>: %email%</p>
			<p><b>Ghi chú</b>: %note%</p>
			<p><b>Ngày đặt thuê</b>: %post_date%</p>
			<p><b>Mã yêu cầu đặt thuê</b>: %id%</p>";
			$search=array("%name%", 
			"%email%",
			"%phone%",
			"%note%",
			"%post_date%",
			"%id%"
			);
			$replace=array($rs_request->fullname,
			$rs_request->email,
			$rs_request->phone, 
			$rs_request->content,
			_ab_dateFormat($rs_request->request_date, false, true),
			$rs_request->id,
			);
			return str_replace($search, $replace, $body);
		}
		
		/**
		* This function return true when room availeble
		* @author trungdt
		* @since Nov 10, 2011
		* @param mixed $item
		*/
		static function _CheckRoomAvaileble($item)
		{
			global $db;
			$sql="SELECT * FROM {$db->prefix}calendar 
			WHERE (location_data = '{$item->location_data}') 
			AND (category_id = {$item->cat_id})";
		}
	}

?>
